﻿/**
 * 定义表单类型。
 */
Ext.define('Happy.metadata.template.DefineForm', {
    alias: 'template.define-form',
    requires: ['Happy.form.metadata.Panel'],

    /**
     * 执行运行时代码生成。
     * 
     * @param {Happy.metadata.ClassGenerator} generator
     * @param {Happy.metadata.Metadata} metadata
     */
    generate: function (generator, metadata) {
        var me = this;

        me.defineForm(generator, metadata);
        me.defineEditFormScene(generator, metadata);
        me.defineNewFormScene(generator, metadata);
    },

    /**
     * 定义表单类型。
     * 
     * @private
     * @param {Happy.metadata.ClassGenerator} generator
     * @param {Happy.metadata.Metadata} metadata
     */
    defineForm: function (generator, metadata) {
        var classConfig = {
            extend: 'Happy.form.metadata.Panel',
            alias: 'widget.' + metadata.getFormClassAlias(),

            metadata: metadata,

            enableScene: true
        };
        Ext.merge(classConfig, metadata.data.form || {});
        Ext.define(metadata.getFormClassName(), classConfig);
    },

    /**
     * 定义表单编辑场景类型。
     * 
     * @private
     * @param {Happy.metadata.ClassGenerator} generator
     * @param {Happy.metadata.Metadata} metadata
     */
    defineEditFormScene: function (generator, metadata) {
        var relationItems = metadata.getDefaultEditFormSceneRelatedItems();
        var classConfig = {
            metadata: metadata,

            getRelatedItems: function (form) {
                return Ext.clone(relationItems);
            }
        };

        if (metadata.isTree()) {
            Ext.apply(classConfig, {
                'config-item-ParentId': {
                    avoidCircular: true
                }
            });
        }

        Ext.merge(classConfig, metadata.data.editForm || {});
        Ext.define(metadata.getEditFormSceneClassName(), classConfig);
    },

    /**
     * 定义表单新建场景类型。
     * 
     * @private
     * @param {Happy.metadata.ClassGenerator} generator
     * @param {Happy.metadata.Metadata} metadata
     */
    defineNewFormScene: function (generator, metadata) {
        var relationItems = metadata.getDefaultNewFormSceneRelatedItems();
        var classConfig = {
            metadata: metadata,

            getRelatedItems: function (form) {
                return Ext.clone(relationItems);
            }
        };
        Ext.merge(classConfig, metadata.data.newForm || {});
        Ext.define(metadata.getNewFormSceneClassName(), classConfig);
    }
});